package com.blockchain.console.servlets.financial.tx;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.WithdrawStatus;
import com.blockchain.console.service.financial.TxglManage;
import com.blockchain.console.servlets.financial.AbstractFinancialServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;

@Right(id = "C_FINANCE_BATCHCHECKSHTG", name = "人民币提现-审核通过-批量审核")
public class BatchCheckShtg extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(final HttpServletRequest request, final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		TxglManage extraction = serviceSession.getService(TxglManage.class);
		String s = request.getParameter("ids");
		int[] ids = new int[0];
		if (!StringHelper.isEmpty(s)) {
			ids = IntegerParser.parseArray(s.split(","));
		}
		String check_reason = request.getParameter("check_reason");
		WithdrawStatus status = EnumParser.parse(WithdrawStatus.class, request.getParameter("status"));
		extraction.checkBatch(status, check_reason, ids);
		if (status == WithdrawStatus.TXCG) {
			sendRedirect(request, response, getController().getURI(request, Txcg.class));
		} else if (status == WithdrawStatus.TXSB) {
			sendRedirect(request, response, getController().getURI(request, Txsb.class));
		}
	}

	@Override
	protected void onThrowable(HttpServletRequest request, HttpServletResponse response, Throwable throwable)
			throws ServletException, IOException {
		if (throwable instanceof ParameterException || throwable instanceof LogicalException) {
			getController().prompt(request, response, PromptLevel.WARRING, throwable.getMessage());
			sendRedirect(request, response, getController().getURI(request, TxglList.class));
		} else {
			super.onThrowable(request, response, throwable);
		}
	}
}
